package com.helloae;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.Feature;
import com.esri.arcgis.geodatabase.FeatureCursor;
import com.esri.arcgis.geodatabase.Fields;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.esriFeatureType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.Cleaner;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 将一条线等分成若干条线，并保存到单独的shp文件。<br>
 * 由于ArcGIS自带的工具最多只能分割成999份，所以弄了这么个东西
 * @author zhushijie
 *
 */
public class SplitLine {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//初始化AE许可
		EngineInitializer.initializeEngine();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
		
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		//这个数据在DataSet/other目录下，请自行更改目录
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile("c:\\temp", 0);
		//加载图层
		IFeatureClass pFeatureClass = pFeatureWorkspace
				.openFeatureClass("oneline.shp");
		IFeature pFeature = pFeatureClass.getFeature(0);
		
		//获得输入图层的Shape_Leng的值，其他字段属性可以用类似方式获取
		Feature inputFeature = (Feature) pFeature;
		double len = (Double) inputFeature.getValue(inputFeature.getFields().findField("Shape_Leng"));
		
		Polyline pLine = (Polyline) pFeature.getShape();
		ArrayList<Polyline> lines = new ArrayList<Polyline>();
		IFields pInFields = pFeatureClass.getFields();
		Fields pFields = new Fields();
		for(int i = 0; i < pFeatureClass.getFields().getFieldCount(); i++){
			if(pInFields.getField(i).getType() != esriFieldType.esriFieldTypeOID || pInFields.getField(i).getType() != esriFieldType.esriFieldTypeGeometry){
				pFields.addField(pInFields.getField(i));
			}
		}
		int m = 10;	//分段的数量
		double dis = pLine.getLength() / m;	//每一段的长度
		for(int i = 0; i < m-1; i++){
			boolean[] splitHappened = new boolean[1];
			int[] newPartIndex = new int[1];
			int[] newSegmentIndex = new int[1];
			
			pLine.splitAtDistance(dis, false, false, splitHappened, newPartIndex, newSegmentIndex);
			Polyline newLine = new Polyline();
			
			for(int j = newSegmentIndex[0]; j < pLine.getSegmentCount(); j++){
				newLine.addSegment(pLine.getSegment(j), null, null);
			}
			
			//删除Polyline后面的子线段
			pLine.removeSegments(newSegmentIndex[0], pLine.getSegmentCount() - newSegmentIndex[0], true);
			
			//保存变更
			pLine.segmentsChanged();
			lines.add(pLine);
			newLine.segmentsChanged();
			pLine = newLine;
			
			//如果是最后一次，则把newLine也加到ArrayList里，否则会少一条线
			if(i == (m-2)){
				lines.add(newLine);
			}
			
			if(i > 0 && i % 200 == 0){
				System.out.print("i = " + i + "\t");
			}
			if(i > 0 && i % 1000 == 0){
				System.out.println("");
			}
		}
		
		//创建featureClass，即创建shapefile
		IFeatureClass outFeatureClass = pFeatureWorkspace.createFeatureClass("splitedLines", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
		
        FeatureCursor featureCursor = new FeatureCursor(outFeatureClass.IFeatureClass_insert(true));
        Feature featureBuffer = (Feature) outFeatureClass.createFeatureBuffer();
        for(int i = 0; i < lines.size(); i++){
        	featureBuffer.setShapeByRef(lines.get(i));
        	//给Shape_Leng字段赋值
        	featureBuffer.setValue(featureBuffer.getFields().findField("Shape_Leng"), len);
        	featureCursor.insertFeature(featureBuffer);
        }
        featureCursor.flush();
        Cleaner.release(featureCursor);
        
		ao.shutdown();
		System.out.println("Congratulaction!");
	}

}
